package sg.edu.nus.iss.vmcs.customer;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import sg.edu.nus.iss.vmcs.store.CashStore;
import sg.edu.nus.iss.vmcs.store.Coin;
import sg.edu.nus.iss.vmcs.store.Store;

/*
 * Copyright 2003 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

/**
 * This interface object is part of the Customer Panel. It is used to enter
 * Coins into the vending machine
 * 
 * 
 * @version 1.1 02 Jan 2008
 * @author justin
 * 
 * @version 1.0 11 Oct 2008
 * @author Toh Kim Huat
 * 
 */
public class CoinInputBox extends Panel {
	
	private static final String TITLE_LABEL = "Enter Coins Here";
	private static final String LABEL_INVALID = "Invalid";
	
	/**
	 * Generated serial version uid
	 */
	private static final long serialVersionUID = -854262451373875919L;

	/**
	 * Array of the weights of the Coins represented by each button in the Coin
	 * Input Box
	 */
	private double[] coinWeights;
	
	/**
	 * Title label
	 */
	private Label title;
	
	/**
	 * Items
	 */
	private List<Button> items;
	
	/**
	 * This constructor creates an instance of the object
	 * 
	 * @param customerPanel
	 *            The container of this object
	 *            
	 *  @author justin - Adapter pattern implementation for 'Enter coin' use case.
	 *  			   - Added a new parameter coinInputBoxAdapter.
	 *  			   - Removed coinReceiver 
	 *  @param coinInputBoxAdapter
	 *  		  Adapter to communicate with CoinReceiver
	 */
	public CoinInputBox(CustomerPanel customerPanel, CoinInputBoxTarget coinInputBoxAdapter) {
		this.setLayout(new GridLayout(0, 1));
		
		// set up the title panel
		Panel titlePanel = new Panel(new BorderLayout());
		title = new Label(TITLE_LABEL);
		title.setAlignment(Label.LEFT);
		title.setFont(new Font("Helvetica", Font.PLAIN, 12));
		titlePanel.add(title,BorderLayout.WEST);
		
		// set up the coin weights
		CashStore cashStore = (CashStore) coinInputBoxAdapter.getTransactionController().getMainController()
				.getStoreController().getStore(Store.CASH);
		
		this.coinWeights = new double[cashStore.getStoreSize()];
		for (int i = 0; i < cashStore.getStoreSize(); i++) {
			this.coinWeights[i] = ((Coin) cashStore.getStoreItem(i)
					.getContent()).getWeight();
		}
		
		// set up the coin buttons panel
		this.items = new ArrayList<Button>();
		ActionListener coinInputActionListener = (ActionListener) coinInputBoxAdapter;
		Panel coinPanel = new Panel(new GridLayout(1, 0));
		for (int i = 0; i < cashStore.getStoreSize(); i++) {
			Button coinButton = new Button(cashStore.getStoreItem(i).getContent().getName());
			coinButton.setActionCommand(String.valueOf(coinWeights[i]));
			coinButton.addActionListener(coinInputActionListener);
			coinPanel.add(coinButton);
			this.items.add(coinButton);
		}
		Button invalidButton = new Button(LABEL_INVALID);
		invalidButton.setActionCommand(String.valueOf(-1));
		invalidButton.addActionListener(coinInputActionListener);
		coinPanel.add(invalidButton);
		this.items.add(invalidButton);
		
		this.add(titlePanel);
		this.add(coinPanel);
		
	}
	
	/**
	 * This operation activates the Coin Input Box if the parameter is TRUE.
	 * Otherwise, the Coin Input Box is deactivated.
	 * 
	 * @param active Boolean to indicate whether Coin Input Box is active.
	 */
	public void setActive(boolean active) {
		System.out.println("CoinInputBox::setActive()");
		for (int i = 0; i < items.size(); i++) {
			this.items.get(i).setEnabled(active);
		}
	}

}
